﻿/* 
* Copyright (c) 2010, The University of Texas at Austin
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, 
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, 
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, 
* this list of conditions and the following disclaimer in the documentation and/or other materials 
* provided with the distribution.
*
* Neither the name of The University of Texas at Austin nor the names of its contributors may be 
* used to endorse or promote products derived from this software without specific prior written 
* permission.
* 
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Xml.Linq;
using Bio.Data.Interfaces;

namespace Bio.Data.Providers.Structure
{
    public class RNASecondaryStructureNucleotideVector : IVector
    {
        public static RNASecondaryStructureNucleotideVector FromXML(XElement vector)
        {
            if (vector != null &&
                !vector.IsEmpty)
            {
                int ntIdx = -1;
                double xpos = -1.0;
                double ypos = -1.0;
                //double rayangle = 0.0;
                //double magnitude = 0.0;

                var ntidxxml = vector.Element(RNAStructureModelXMLTags.StructureModelRenderingVectorNucleotideIndexLabel);
                var ntxposxml = vector.Element(RNAStructureModelXMLTags.StructureModelRenderingVectorXLabel);
                var ntyposxml = vector.Element(RNAStructureModelXMLTags.StructureModelRenderingVectorYLabel);
                //var ntrayanglexml = vector.Element(RNAStructureModelXMLTags.StructureModelRenderingVectorRayAngleLabel);
                //var ntmagnitudexml = vector.Element(RNAStructureModelXMLTags.StructureModelRenderingVectorMagnitudeLabel);

                RNASecondaryStructureNucleotideVector retValue = new RNASecondaryStructureNucleotideVector();

                if (ntidxxml != null &&
                    !ntidxxml.IsEmpty &&
                    Int32.TryParse(ntidxxml.Value, out ntIdx))
                {
                    retValue.NtIndex = (ntIdx - 1);

                    if (ntxposxml != null &&
                        !ntxposxml.IsEmpty &&
                        Double.TryParse(ntxposxml.Value, out xpos))
                    {
                        retValue.StartX = xpos;
                    }

                    if (ntyposxml != null &&
                        !ntyposxml.IsEmpty &&
                        Double.TryParse(ntyposxml.Value, out ypos))
                    {
                        retValue.StartY = ypos;
                    }

                    /*if(ntrayanglexml != null &&
                       !ntrayanglexml.IsEmpty &&
                       Double.TryParse(ntrayanglexml.Value, out rayangle))
                    {
                        retValue.RayAngle = rayangle;
                    }

                    if (ntmagnitudexml != null &&
                        !ntmagnitudexml.IsEmpty &&
                        Double.TryParse(ntmagnitudexml.Value, out magnitude) &&
                        magnitude >= 0.0)
                    {
                        retValue.Magnitude = magnitude;
                    }*/

                    return retValue;
                }
                else { return null; }
            }
            else { return null; }
        }

        #region IVector Members

        public double RayAngle { get; set; }
        public double Magnitude { get; set; }
        public double StartX { get; set; }
        public double StartY { get; set; }

        public IVector Clone()
        {
            return new RNASecondaryStructureNucleotideVector()
            {
                RayAngle = this.RayAngle,
                Magnitude = this.Magnitude,
                StartX = this.StartX,
                StartY = this.StartY
            };
        }

        #endregion

        public RNASecondaryStructureNucleotideVector()
        {
        }

        public RNASecondaryStructureNucleotideVector(double startx, double starty, double endx, double endy)
        {
            CalculateVectorFromPoints(startx, starty, endx, endy);
        }

        public RNASecondaryStructureNucleotideVector(double startx, double starty)
        {
            this.StartX = startx;
            this.StartY = starty;
        }

        public int NtIndex { get; set; }

        private void CalculateVectorFromPoints(double startx, double starty, double endx, double endy)
        {
            StartX = startx;
            StartY = starty;
            RayAngle = Math.PI + Math.Atan2((StartY - endy), (StartX - endx));
            Magnitude = Math.Sqrt(Math.Pow((StartY - endy), 2) + Math.Pow((StartX - endx), 2));
        }
    }
}
